package com.pingancar.creditmanage.dao;

import com.pingancar.creditmanage.dao.BaseDAO;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.util.myenum.ShopInfoField;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
/**
 *Created by system on Tue Sep 09 11:17:13 CST 2014
 */
@Repository
public class ShopInfoDaoImpl extends BaseDAO<ShopinfoPojo> {

	public Integer save(ShopinfoPojo shopinfo){
		return (Integer)super.save(shopinfo);
	}

    public List<ShopinfoPojo> getShopList(Integer type, Integer offset, Integer length, String name) {
        String hql = "select distinct ps.shopinfo from PaservicePojo ps where ps.stop = 0 ";
        if (type != -1)
            hql += "and ps.type.id = :type ";
        if (!name.equals(""))
            hql += "and ps.shopinfo.name like :name";
        Query q = getSession().createQuery(hql);
        if (type != -1)
            q.setInteger("type", type);
        if (!name.equals(""))
            q.setString("name", "%"+name+"%");

        return q.setFirstResult(offset)
                .setMaxResults(length)
                .list();
    }

    /**
     * 不按服务查询
     */
    public List getAllShopList(Integer offset, Integer length, String name) {
        String hql = "select si from ShopinfoPojo si where 1 = 1 ";
        if (!name.equals(""))
            hql += "and si.name like :name";
        Query q = getSession().createQuery(hql);
        if (!name.equals(""))
            q.setString("name", "%"+name+"%");

        return q.setFirstResult(offset).setMaxResults(length).list();
    }

    public Long getCountOfAllShopList(String name) {
        String hql = "select count(si.id) from ShopinfoPojo si where 1 = 1 ";
        if (!name.equals(""))
            hql += "and si.name like :name";

        Query q = getSession().createQuery(hql);
        if (!name.equals(""))
            q.setString("name", "%"+name+"%");

        return (Long)q.uniqueResult();
    }
}